package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.construction.api.BrandApi;
import com.ruyuan.eshop.construction.domain.dto.BrandListDTO;
import com.ruyuan.eshop.construction.domain.dto.BrandResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryBrandRequest;
import com.ruyuan.eshop.construction.domain.request.BrandRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 品牌服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = BrandApi.class, retries = 0)
public class BrandApiImpl implements BrandApi {

    @Autowired
    private BrandService brandService;

    @Override
    public JsonResult<BrandResultDTO> brand(BrandRequest request) {
        try {
            BrandResultDTO brandResultDTO = brandService.brand(request);
            return JsonResult.buildSuccess(brandResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<BrandListDTO> getBrandInfo(QueryBrandRequest request) {

        try {
            BrandListDTO brandListDTO = brandService.getBrandInfoByLike(request);
            return JsonResult.buildSuccess(brandListDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
